(require 'tcp-client)

(defun sample-error-report(buffer server port error)
  (save-excursion
    (set-buffer buffer)
    (insert (format "[error] %s:%i -- %s\n" server port error))))

(defun sample-connection-report(buffer server port)
  (save-excursion
    (set-buffer buffer)
    (insert (format "[connect] %s:%i -- Connection established\n" server port))))

(defun sample-abort-report(buffer server port)
  (save-excursion
    (set-buffer buffer)
    (insert (format "[error] %s:%i -- Abort connection\n" server port))))

(defun sample-sentinel-report(process event)
  (save-excursion
    (set-buffer (process-buffer process))
    (insert (format "[event] Process: %s had the event -- %s" process event))))

(defun sample-filter-report(process message)
  (save-excursion
    (set-buffer (process-buffer process))
    (insert (format "[got] %s" message))))

(defun sample-send (data)
    (interactive "sSend data: ")
  (tcp-send (get-buffer-process (get-buffer-create (buffer-name))) data))


(defun sample-create (sserver sport)
  (interactive "sserver: \nnport: ")
  (tcp-connect "*tcp-sample*"
               (make-new-tcp-connection :server sserver :port sport)
               (make-new-tcp-hooks
                :connection-failed-handler 'sample-error-report
                :connection-established-handler 'sample-connection-report
                :connection-abort-handler 'sample-abort-report
                :sentinel-handler 'sample-sentinel-report
                :filter-handler 'sample-filter-report))
  ;;(switch-to-buffer "*tcp-sample*")
  )

(provide 'tcp-sample)

